package hut.game;

import hut.game.actor.Computer;
import hut.game.actor.Player;
import hut.game.sound.Music;
import hut.game.sound.Music_main;
import hut.game.table.BanCo;
import hut.game.table.VanCo;
import hut.game.unit.Chot;
import hut.game.unit.Ma;
import hut.game.unit.Phao;
import hut.game.unit.QuanCo;
import hut.game.unit.Sy;
import hut.game.unit.Tinh;
import hut.game.unit.Xe;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class GameActivity extends Activity implements OnTouchListener,OnClickListener {
	public static RelativeLayout rootGame;
	public static Context gameContext;
	public static OnTouchListener mOnTouch;
	public static ImageView imgTurn;
	Button btnOptions;
	Button btnUndo;
	Button btnExit;
	public static MyCustomDialog ThongBaoKetThuc;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		gameContext = getBaseContext();
		mOnTouch = this;
		ThongBaoKetThuc = new MyCustomDialog(this);
		setContentView(R.layout.layout_game);
		rootGame = (RelativeLayout)findViewById(R.id.rootGame);
		imgTurn = (ImageView)findViewById(R.id.img_turn);
		btnOptions = (Button)findViewById(R.id.btn_option);
		btnUndo = (Button)findViewById(R.id.btn_undo);
		btnExit = (Button)findViewById(R.id.btn_exit_game);
		btnExit.setOnClickListener(this);
		btnUndo.setOnClickListener(this);
		btnOptions.setOnClickListener(this);
		BanCo.KhoiTao(gameContext, this);
		if (!VanCo.resume) {
			if (VanCo.state) {
				VanCo.LuotDi = 0;
				VanCo.DanhDau = null;
				Toast toast = Toast.makeText(getBaseContext(), "Red move first!", 2000);
				toast.setGravity(Gravity.BOTTOM, 0,	(int) (200 * MainActivity.mScale));
				toast.show();
				VanCo.n1 = new Player(0, this);
				setLayout(VanCo.n1);
				VanCo.n2 = new Player(1, this);
				setLayout(VanCo.n2);
			} else {
				VanCo.LuotDi = 0;
				VanCo.ply = 0;
				Toast toast = Toast.makeText(getBaseContext(), " Computer move first!", 2000);
				toast.setGravity(Gravity.BOTTOM, 0,	(int) (200 * MainActivity.mScale));
				toast.show();
				VanCo.DangChoi = true;
				VanCo.computer = new Computer(0);
				setLayout(VanCo.computer);
				VanCo.n1 = new Player(1, this);
				setLayout(VanCo.n1);
				
				Computer.ComputerThink();
			}
		} else {
			VanCo.Resume();
			if (VanCo.LuotDi == 0) {
				Toast toast = Toast.makeText(getBaseContext(), "Red move!", 2000);
				toast.setGravity(Gravity.BOTTOM, 0,	(int) (200 * MainActivity.mScale));
				toast.show();
			} else {
				Toast toast = Toast.makeText(getBaseContext(), "Black move!", 2000);
				toast.setGravity(Gravity.BOTTOM, 0,	(int) (200 * MainActivity.mScale));
				toast.show();
			}
			if (VanCo.state) {
				setLayout(VanCo.n1);
				setLayout(VanCo.n2);
			} else {
				setLayout(VanCo.n1);
				setLayout(VanCo.computer);
				
				Computer.ComputerThink();
			}
		}
		setLayoutCanMove();
	}

	@Override
	protected void onResume() {
		super.onResume();
		VanCo.DangChoi = true;
		Music.stop(gameContext);
		if (Option.getMusic(gameContext)) {
			Music_main.play_main(gameContext, R.raw.a);
		} else {
			Music_main.stop_main(gameContext);
		}
	}

	private void setLayoutCanMove() {
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				rootGame.addView(BanCo.ViTri[i][j].imgMove);
			}
		}
	}

	private void setLayout(Player p) {
		for (int i = 0; i < p.dsQuanCo.size(); i++) {
			rootGame.addView(p.dsQuanCo.get(i).imgUnit);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		VanCo.DangChoi = false;
		Music_main.stop_main(this);
		SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
		String myListPreference = myPreference.getString("level","2");
		if (!VanCo.state) {
			Computer.deep = Integer.parseInt(String.valueOf(myListPreference));
		}
		if (Option.getMusic(gameContext)) {
			Music_main.play_main(gameContext, R.raw.a);
		} else {
			Music_main.stop_main(gameContext);
		}
	}

	void free()
	{
		Log.d("free", "free data");
		VanCo.save();
		VanCo.resume = false;
		VanCo.DangChoi = false;
		VanCo.ResetVanCo();
		BanCo.ResetBanCo();
		Music_main.stop_main(this);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			free();
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	

	public static void showResult() {
		ThongBaoKetThuc.show();
	}

	// Hàm add view vào activity
	public static void setLayout(View v, float left, float top, float right,
			float bottom) {
		RelativeLayout.LayoutParams layoutparam = new RelativeLayout.LayoutParams(v.getLayoutParams().width, v.getLayoutParams().height);
		layoutparam.setMargins((int) left, (int) top, (int) right, (int) bottom);
		v.setLayoutParams(layoutparam);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			Log.d("id", "id: " + v.getId());
			if (v.getId() >= 40 && v.getId() <= 130) {
				for (int i = 0; i <= 9; i++) {
					for (int j = 0; j <= 8; j++) {
						if (BanCo.ViTri[i][j].imgMove.getId() == v.getId()) {
							BanCo.imgMoveClick(BanCo.ViTri[i][j].imgMove,BanCo.ViTri[i][j].imgMove.getId());
							if(!VanCo.state)
								Computer.ComputerThink();
						}
					}
				}
			} else if(v.getId() >= 0 && v.getId() <= 32){
				if (VanCo.state) {
					if(VanCo.LuotDi == 0){
						for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
							if (VanCo.n1.dsQuanCo.get(i).imgUnit.getId() == v.getId()){
								Log.d("qc", VanCo.n1.dsQuanCo.get(i).TrangThai + "");
								if(VanCo.n1.dsQuanCo.get(i).TrangThai == 2){
									FirstMove(VanCo.n1.dsQuanCo.get(i));
								}else {
									VanCo.n1.dsQuanCo.get(i).imgUnitClick(VanCo.n1.dsQuanCo.get(i).imgUnit);
								}
							}
						}
					}else{
						for (int i = 0; i < VanCo.n2.dsQuanCo.size(); i++) {
							if (VanCo.n2.dsQuanCo.get(i).imgUnit.getId() == v.getId()){
								Log.d("qc", VanCo.n2.dsQuanCo.get(i).TrangThai + "");
								if(VanCo.n2.dsQuanCo.get(i).TrangThai == 2){
									FirstMove(VanCo.n2.dsQuanCo.get(i));
								}else {
									Log.d("tuongclick", "click here " + VanCo.n2.dsQuanCo.get(i).Ten + "/" + v.getId());
									VanCo.n2.dsQuanCo.get(i).imgUnitClick(VanCo.n2.dsQuanCo.get(i).imgUnit);
								}
							}
						}
					}
				} else {
					for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
						if (VanCo.n1.dsQuanCo.get(i).imgUnit.getId() == v.getId()){
							Log.d("qc", VanCo.n1.dsQuanCo.get(i).TrangThai + "/" + VanCo.n1.dsQuanCo.get(i).Ten);
							if(VanCo.n1.dsQuanCo.get(i).TrangThai == 2){
								FirstMove(VanCo.n1.dsQuanCo.get(i));
							}else {
								VanCo.n1.dsQuanCo.get(i).imgUnitClick(VanCo.n1.dsQuanCo.get(i).imgUnit);
							}
						}
					}
				}
			}
		}
		return true;
	}
	
	private void FirstMove(QuanCo qc){
		if(qc.TenQuanChuaLat == "sy") {
			Sy tempSy = new Sy();
			tempSy.KhoiTao(qc.Phe, qc.Ten, qc.ThuTu, qc.TrangThai, qc.Khoa, qc.Hang, qc.Cot);
			qc.imgUnitChuaLatClick(qc.imgUnit, tempSy);
		}
		if(qc.TenQuanChuaLat == "tinh") {
			Tinh tempTinh = new Tinh();
			tempTinh.KhoiTao(qc.Phe, qc.Ten, qc.ThuTu, qc.TrangThai, qc.Khoa, qc.Hang, qc.Cot);
			qc.imgUnitChuaLatClick(qc.imgUnit, tempTinh);
		}
		if(qc.TenQuanChuaLat == "xe") {
			Xe tempXe = new Xe();
			tempXe.KhoiTao(qc.Phe, qc.Ten, qc.ThuTu, qc.TrangThai, qc.Khoa, qc.Hang, qc.Cot);
			qc.imgUnitChuaLatClick(qc.imgUnit, tempXe);
		}
		if(qc.TenQuanChuaLat == "phao") {
			Phao tempPhao = new Phao();
			tempPhao.KhoiTao(qc.Phe, qc.Ten, qc.ThuTu, qc.TrangThai, qc.Khoa, qc.Hang, qc.Cot);
			qc.imgUnitChuaLatClick(qc.imgUnit, tempPhao);
		}
		if(qc.TenQuanChuaLat == "ma") {
			Ma tempMa = new Ma();
			tempMa.KhoiTao(qc.Phe, qc.Ten, qc.ThuTu, qc.TrangThai, qc.Khoa, qc.Hang, qc.Cot);
			qc.imgUnitChuaLatClick(qc.imgUnit, tempMa);
		}
		if(qc.TenQuanChuaLat == "chot") {
			Chot tempChot = new Chot();
			tempChot.KhoiTao(qc.Phe, qc.Ten, qc.ThuTu, qc.TrangThai, qc.Khoa, qc.Hang, qc.Cot);
			qc.imgUnitChuaLatClick(qc.imgUnit, tempChot);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_undo:
			if(!VanCo.Finish){
				if (VanCo.state)
					VanCo.Undo();
				else{
					VanCo.Undo();
					VanCo.Undo();
				}
			}else{
				Toast.makeText(this, "End Game!", 2000).show();
			}
			break;
		case R.id.btn_option:
			startActivity(new Intent(GameActivity.this, Option.class));
			break;
		case R.id.btn_exit_game:
			free();
			finish();
			break;
		default:
			break;
		}
	}
}
